// pages/singImg/singImg.js
import Notify from '@vant/weapp/notify/notify';
import Toast from '@vant/weapp/toast/toast';

import Api from "../../utils/api"
import Ajax from "../../utils/ajax"

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    message:'',
    addressInfo:{
      latitude: '',
      longitude: '',
      address:'武汉'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      addressInfo:app.globalData.addressInfo
    })
  },

  // 提交
  submit(){
    if(!this.data.message){
      Notify({ type: 'warning', message: '请输入留言' });
      return
    }
    let nd = {
      openid: app.globalData.openid,
      userName: app.globalData.userInfo.nickName,
      address: app.globalData.addressInfo.address,
      longitude: app.globalData.addressInfo.longitude,
      latitude: app.globalData.addressInfo.latitude,
      type:5,
      msg: this.data.message,
      img: ''
    }
    Toast.loading({
      message: '加载中...',
      duration:0,
      forbidClick: true,
    });
    let i = app.globalData.signNumber
    Ajax.post(Api.wx.signImg,nd).then(res=>{
      Toast.success('打卡成功！');
      // 打卡数加一
      i++
      app.globalData.signNumber = i
      wx.redirectTo({
        url: '../shareLocation/shareLocation?scene='+ encodeURIComponent(res),
      })
    })
    let userInfo = {
      openid: app.globalData.openid,
      nickName: app.globalData.userInfo.nickName,
      avatarurl: app.globalData.userInfo.avatarUrl,
      gender: app.globalData.userInfo.gender || 0,
      city: app.globalData.userInfo.city,
    }
    Ajax.post(Api.wx.login,userInfo)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})